# FILE: GeoLocator.py
# DESC: geonames full text search broker
#

from HttpReader import HttpReader, HttpDocumentNotFound, HttpReadError

from django.utils import simplejson

import sys

class WebServiceBroker:

	def requestJSON(self, url):
		try:
			httpReader = HttpReader()
			data = httpReader.read(url)
			try:
				rv = simplejson.loads(unicode(data, 'utf-8'))
			except UnicodeDecodeError:
				rv = simplejson.loads(unicode(data, '1250'))
			
			return rv
		
		except HttpDocumentNotFound, e:
			sys.stderr.write("HttpDocumentNotFound exception caught.")
			raise
		except HttpReadError, e:
			sys.stderr.write("HttpReadError exception caught, reason: %s" % e)
			raise
		except Exception, e:
			sys.stderr.write("an exception caught, reason: %s" % e)
			raise e
	
	def dump(self):
		return __dict__
